package com.select.course.controller;


import com.select.course.entity.LoginUser;
import com.select.course.service.UserStudentService;
import com.select.course.utils.PageUtils;
import com.select.course.utils.R;
import com.select.course.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
@Slf4j
public class StudentController {

    @Resource
    private UserStudentService userStudentService;


    @GetMapping("/getcurri/{courseId}")
    @PreAuthorize("hasAnyRole('root','teacher','student')")
    public R getCuuri(@PathVariable String courseId) {
        CourseVo courseVo = userStudentService.getCuuri(courseId);

        return R.ok().put("data", courseVo);
    }

    @GetMapping("/getxuefenxuan/{userId}")
    @PreAuthorize("hasAnyRole('root','teacher','student')")
    public R getXueFenXuan(@PathVariable String userId) {
        SumarizeVo xueFenXuna = userStudentService.getXueFenXuna(userId);
        return R.ok().put("data", xueFenXuna);
    }

    @GetMapping("/getxuefenbi/{userId}")
    @PreAuthorize("hasAnyRole('root','teacher','student')")
    public R getXueFenBioa(@PathVariable String userId) {
        SumarizeVo sumarizeVo = userStudentService.getXueFenBiao(userId);
        return R.ok().put("data", sumarizeVo);
    }

    @DeleteMapping("/deleteselection/{courseId}/{userId}")
    @PreAuthorize("hasAnyRole('root','teacher','student')")
    public R deleteCourse(@PathVariable String courseId, @PathVariable String userId) {
        LoginUser instance = LoginUser.getInstance();
        try {
            userStudentService.deleteCourse(courseId, userId);
            log.info("{}退课{}成功", instance.getUser().getIdNumber(), courseId);
            return R.ok();
        } catch (RuntimeException e) {
            log.error("{}退课{}失败!", instance.getUser().getIdNumber(), courseId);
        }
        return R.error();

    }

    @GetMapping("/getkechengbiao/{userId}")
    public R getKeChengBiao(@PathVariable String userId) {
        List<CourseVo> courseVoList = userStudentService.getKeChengBiao(userId);
        return R.ok().put("data",courseVoList);
    }

    @PostMapping("/login_student")
    public R loginStudent(@RequestBody StudentVo studentVo) {

        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userStudentService.login(studentVo);
            log.info("用户{}登陆成功！", instance.getUser().getIdNumber());
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}登录失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @GetMapping("/loginout_student")
    public R loginoutStudent() {
        LoginUser instance = LoginUser.getInstance();
        try {
            log.info("用户{}登录成功！", instance.getUser().getIdNumber());
            instance.setAuthorities(null);
            instance = null;
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}登录失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @PostMapping("/getselectionself")
    public R getSelectedCourse(@RequestBody CoursePageVo coursePageVo) {
        PageUtils pageUtils = userStudentService.getSelectedCourse(coursePageVo);

        return R.ok().put("data", pageUtils);
    }

    @GetMapping("/addsutclass/{courseId}/{idNumber}")
    @PreAuthorize("hasRole('student')")
    public R selectCourse(@PathVariable String courseId, @PathVariable String idNumber) {
        try {
            userStudentService.selectCourse(courseId, idNumber);
            log.info("用户{}选课{}成功！", idNumber, courseId);
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}选课{}失败！", idNumber, courseId);
        }
        return R.error();
    }

    @PostMapping("/studentlist")
    public R getStudentList(@RequestBody PageVo pageVo) {
        PageUtils studentList = userStudentService.getStudentList(pageVo);

        return R.ok().put("data", studentList);

    }

    @PutMapping("/updatestu")
    @PreAuthorize("hasAnyRole('student')")
    public R updateStudent(@RequestBody StudentVo studentVo) {
        LoginUser instance = LoginUser.getInstance();
        try {
            userStudentService.updateStudent(studentVo);
            log.info("用户{}更新用户{}成功，新信息为{}", instance.getUser().getIdNumber(), studentVo.getUserId(), studentVo);
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}更新用户{}失败！", instance.getUser().getIdNumber(), studentVo.getUserId());
        }
        return null;
    }

    @GetMapping("/getstu/{userId}")
    public R getStuByIdNumber(@PathVariable String userId) {
        StudentVo studentVo = userStudentService.getStuByIdNumber(userId);

        return R.ok().put("data", studentVo);
    }
//    @GetMapping("/getself/{userId}")
//    @PreAuthorize("hasAnyRole('root','student')")
//    public R getSelf(@PathVariable String userId) {
//        StudentVo studentVo = userStudentService.getStudentSelf(userId);
//
//        return R.ok().put("data", studentVo);
//    }

    // admin/addstu', this.addStuForm
    @PostMapping("/addstu")
    @PreAuthorize("hasRole('root')")
    public R addStudent(@RequestBody StudentVo studentVo) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userStudentService.addStudent(studentVo);
            log.info("用户{}添加用户{}成功，新用户为{}", instance.getUser().getIdNumber(), studentVo.getUserId(), studentVo);
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}添加用户{}失败！", instance.getUser().getIdNumber(), studentVo.getUserId());
        }
        return null;
    }

    @PreAuthorize("hasRole('root')")
    @PostMapping("importData")
    public R importData(MultipartFile file) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userStudentService.importData(file);
//            if (r.get("successCount").equals("0")) {
//                log.error("用户{}导入学生信息失败！",instance.getUser().getIdNumber());
//                return R.error();
//            }
            int successCount = (Integer) r.get("successCount");
            log.info("用户{}导入{}条学生信息成功！", instance.getUser().getIdNumber(), successCount);
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}导入学生信息失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }


    @PreAuthorize("hasRole('root')")
    @GetMapping(value = "/exportData")
    public R exportData(HttpServletResponse response) {
        LoginUser instance = LoginUser.getInstance();
        try {
            userStudentService.exportData(response);
            log.info("用户{}导出学生信息成功", instance.getUser().getIdNumber());
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}导出学生信息失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

//    @PostMapping("/login_student")
//    public R loginStudent(@RequestBody UserStudent userStudent) {
//        userStudentService.login(userStudent);
//
//        return R.ok();
//    }

    ////admin/deletestu/' + xuehao
    @DeleteMapping("deletestu/{userId}")
    @PreAuthorize("hasRole('root')")
    public R deleteStudent(@PathVariable String userId) {

        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userStudentService.deleteStudent(userId);
            log.info("用户{}删除用户{}成功！", instance.getUser().getIdNumber(), userId);
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}删除用户{}失败！", instance.getUser().getIdNumber(), userId);
        }
        return R.error();
    }

//    @GetMapping("/list/student")
//    public List<AllSchollPeopleTo> list() {
////        List<UserStudent> list = userStudentService.list();
////        List<AllSchollPeopleTo> collect = list.stream().map(e -> {
////            AllSchollPeopleTo allSchollPeopleTo = new AllSchollPeopleTo();
////            allSchollPeopleTo.setCollege(e.getCollegeId());
////            allSchollPeopleTo.setName(e.getStudentName());
////            return allSchollPeopleTo;
////        }).collect(Collectors.toList());
////        return collect;
//
//    }
}
